package com.nogroup.springbootproject.controller;

import com.nogroup.springbootproject.dto.CclassDto;
import com.nogroup.springbootproject.entity.Cclass;
import com.nogroup.springbootproject.service.CclassService;
import com.nogroup.springbootproject.utils.PageUtils;
import com.nogroup.springbootproject.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Cclass)表控制层
 *
 * @author makejava
 * @since 2024-04-07 19:03:50
 */
@RestController
@RequestMapping("cclass")
public class CclassController {
    /**
     * 服务对象
     */
    @Resource
    private CclassService cclassService;

    /**
     * 分页查询
     *
     * @param cclass 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Cclass>> queryByPage(Cclass cclass, PageRequest pageRequest) {
        return ResponseEntity.ok(this.cclassService.queryByPage(cclass, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Cclass> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.cclassService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param cclass 实体
     * @return 新增结果
     */
    @RequestMapping("classInsert")
    @PostMapping
    public DataResult add(Cclass cclass) {
        return DataResult.successByData(this.cclassService.insert(cclass));
    }

    /**
     * 编辑数据
     *
     * @param cclass 实体
     * @return 编辑结果
     */
    @RequestMapping("edit")
    @PutMapping
    public DataResult edit(Cclass cclass) {
        return DataResult.successByData(this.cclassService.update(cclass));
    }

    /**
     * 删除数据
     *
     * @param cclass 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteById")
    @DeleteMapping
    public DataResult deleteById(Cclass cclass) {
        this.cclassService.deleteById(cclass);
        return DataResult.successByData(cclass);
    }

    @RequestMapping("getAllClasses")
    public DataResult getAllClasses(){
        System.out.println("12345");
        List<Cclass> classes = cclassService.getAllClasses();
        System.out.println("输出class内容：" + classes);
        return DataResult.successByDatas(classes);
    }


    @RequestMapping("getClassesByFuzzySearch")
    public DataResult getClassesByFuzzySearch(CclassDto cclassDto){
        long count = cclassService.countPage(cclassDto);
        int index = PageUtils.getStartPage(cclassDto.getPage(), cclassDto.getLimit());
        cclassDto.setPage(index < count ? index : 0);
        List<Cclass> classes = cclassService.getClassesByFuzzySearch(cclassDto);

        return DataResult.successByTotalData(classes,count);
    }
}

